package shayu.member.service.impl;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import shayu.member.dao.MemberDao;
import shayu.member.dao.MemberLevelDao;
import shayu.member.entity.MemberEntity;
import shayu.member.entity.MemberLevelEntity;
import shayu.member.exeception.PhoneExistException;
import shayu.member.exeception.UsernameExistException;
import shayu.member.service.MemberService;
import shayu.member.vo.MemberLoginVo;
import shayu.member.vo.MemberRegistVo;

import javax.annotation.Resource;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Resource
    private MemberLevelDao memberLevelDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void regist(MemberRegistVo vo) {
        MemberEntity memberEntity = new MemberEntity();

        // 检查会员等级
        //this.baseMapper.getDefalutLevel(vo);
        //设置默认等级
        MemberLevelEntity levelEntity = memberLevelDao.getDefaultLevel();
        memberEntity.setLevelId(levelEntity.getId());

        // 检查用户名和手机号是否唯一,不唯一的话抛异常
        checkPhoneUnique(vo.getPhone());
        checkUsernameUnique(vo.getUsername());

        memberEntity.setNickname(vo.getUsername());
        memberEntity.setUsername(vo.getUsername());

        // 要对密码进行加密储存
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(vo.getPassword());
        memberEntity.setPassword(encode);
        memberEntity.setGender(0);
        memberEntity.setCreateTime(new Date());
        this.baseMapper.insert(memberEntity);

    }

    @Override
    public void checkPhoneUnique(String phone) throws PhoneExistException {
        MemberDao dao = this.baseMapper;
        Long mobile = dao.selectCount(new QueryWrapper<MemberEntity>().eq("mobile", phone));
        if(mobile>0){
            throw new PhoneExistException();
        }
    }

    @Override
    public void checkUsernameUnique(String username) throws UsernameExistException {
        Long usernameocunt = this.baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("username", username));
        if(usernameocunt>0){
            throw new  UsernameExistException();
        }
    }

    @Override
    public MemberEntity login(MemberLoginVo vo) {
        MemberDao dao = this.baseMapper;
        String username = vo.getUsername();
        String password = vo.getPassword();

         // 1. 去数据库查询存不存在用户
        MemberEntity usernamefromdb = dao.selectOne(new QueryWrapper<MemberEntity>().eq("username", username).eq("mobile",username));
        if(usernamefromdb==null){
            // 登录失败
            return null;
        }else{
            // 2. 从数据库中取值去比较传递过来的密码
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            String passwordfromdb = usernamefromdb.getPassword();
            // 密码匹配
            boolean matches = passwordEncoder.matches(password, passwordfromdb);
            if(matches=false){
                return null;
            }else{
                return usernamefromdb;
            }
        }

    }


}